//
//  ProjectDetailViewController.m
//  APJ Resource
//
//  Created by Richard Zhao on 2/24/14.
//  Copyright (c) 2014 APJ. All rights reserved.
//

#import "ProjectDetailViewController.h"
#import "HRStore.h"
#import "ProjectAllocation.h"
#import "IQGanttView.h"
#import "Staff.h"

@interface ProjectDetailViewController ()

@end

@implementation ProjectDetailViewController
{
    IQGanttView *gantt;
}
@synthesize projectId;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //self.navigationItem.title = @"Project Resource Allocation";
    //self.navigationController.view.tintColor = [UIColor redColor];
	
    NSArray *colors = [NSArray arrayWithObjects:[UIColor greenColor], [UIColor yellowColor], [UIColor blueColor], [UIColor redColor], [UIColor purpleColor], [UIColor orangeColor], [UIColor grayColor], nil];
    
    CGRect frame = CGRectMake(self.view.bounds.origin.x,
                              self.view.bounds.origin.y + 60,
                              2048,
                              self.view.bounds.size.height);
    
    gantt = [[IQGanttView alloc] initWithFrame:frame];
    //[self.view addSubview:gantt];
    [self setView:gantt];
    
    double ganttWidth = 3105.944824;
    double scope = gantt.scaleWindow.windowEnd - gantt.scaleWindow.windowStart;
    NSArray *projectAssign = [[HRStore sharedStore] projectAssignFroProject:projectId];
    int i=0;
    for (ProjectAllocation *allocation in projectAssign) {
        UIProgressView *pro = [[UIProgressView alloc] init];
        NSTimeInterval start = [[NSDate dateWithTimeIntervalSince1970:allocation.startDate] timeIntervalSinceReferenceDate];
        NSTimeInterval end = [[NSDate dateWithTimeIntervalSince1970:allocation.endDate] timeIntervalSinceReferenceDate];
        double startPoint = start - gantt.scaleWindow.windowStart;
        double endPoint = end - gantt.scaleWindow.windowStart;
        double x1 = (ganttWidth / scope) * startPoint;
        double x2 = (ganttWidth / scope) * endPoint;
        [pro setFrame:CGRectMake(x1, 80*i+80, x2-x1, 100)];
        CGAffineTransform transform = CGAffineTransformMakeScale(1.0f, 3.0f);
        pro.transform = transform;
        [pro setTrackTintColor:[colors objectAtIndex:i%colors.count]];
        [gantt addSubview:pro];
        
        UILabel *staffName = [[UILabel alloc] init];
        Staff *staff = [[HRStore sharedStore] findStaffById:allocation.staffId];
        staffName.text = staff.chineseName;
        [staffName setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
        [staffName sizeToFit];
        [staffName setCenter:CGPointMake(x1+(x2-x1)/2, 80*i+95)];
        [gantt addSubview:staffName];
        
        UILabel *startDate = [[UILabel alloc] init];
        NSDate *sDate = [NSDate dateWithTimeIntervalSince1970:allocation.startDate];
        NSDateFormatter *formate = [[NSDateFormatter alloc] init];
        [formate setDateFormat:@"dd/MMM"];
        startDate.text = [formate stringFromDate:sDate];
        startDate.font = [UIFont italicSystemFontOfSize:12.0];
        [startDate setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
        [startDate sizeToFit];
        [startDate setCenter:CGPointMake(x1-23, 80*i+84)];
        [gantt addSubview:startDate];
        
        UILabel *endDate = [[UILabel alloc] init];
        NSDate *eDate = [NSDate dateWithTimeIntervalSince1970:allocation.endDate];
        endDate.text = [formate stringFromDate:eDate];
        endDate.font = [UIFont italicSystemFontOfSize:12.0];
        [endDate setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
        [endDate sizeToFit];
        [endDate setCenter:CGPointMake(x2+23, 80*i+84)];
        [gantt addSubview:endDate];
        
        i++;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
